// pages/workForm/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    is_manage:false,
    edit:false,
    workdata:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let is_manage = wx.getStorageSync('is_manage')
    this.setData({
      id:options.id,
      is_manage:is_manage
    })
    this.getWorkdetail(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getWorkdetail: function(id){
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.database().collection('internlist').where({_id:id}).get({
      success: (res) => {
        let data = res.data[0]
        data.work_intro = data.work_intro.join('\n')
        data.work_request = data.work_request.join('\n')
        data.salary_detail = data.salary_detail.join('\n')
        this.setData({
          workdata:data
        })
      },
      fail: (err) => {
        console.err(err)
      },
      complete:(res)=>{
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })

  },
  editWorkdetail:function () {
    let workdata = this.data.workdata
    if(!workdata.work_request.length){
      workdata.work_request=['']
    }
    if(!workdata.work_intro.length){
      workdata.work_intro=['']
    }
    if(!workdata.salary_detail.length){
      workdata.salary_detail=['']
    }
    this.setData({
      edit:true,
      workdata:workdata
    })
  },
  inputTitle:function (e) {
    let workdata = this.data.workdata
    workdata.work_title = e.detail.value
    this.setData({
      workdata:workdata
    })
  },
  inputDate:function (e) {
    let workdata = this.data.workdata
    workdata.date = e.detail.value
    this.setData({
      workdata:workdata
    })
  },
  inputsalary:function (e) {
    let workdata = this.data.workdata
    workdata.salary = e.detail.value
    console.log(workdata)
    console.log(e.detail.value)
    this.setData({
      workdata:workdata
    })
  },
  inputlocation:function (e) {
    let workdata = this.data.workdata
    workdata.work_location = e.detail.value
    this.setData({
      workdata:workdata
    })
  },
  inputCompany:function (e) {
    let workdata = this.data.workdata
    workdata.company_name = e.detail.value
    this.setData({
      workdata:workdata
    })
  },
  inputCompanyintro:function (e) {
    let workdata = this.data.workdata
    workdata.intro = e.detail.value
    this.setData({
      workdata:workdata
    })
  },
   // 输入绑定事件
   inputIntro:function (e) {
    let index = e.currentTarget.dataset.index
    let workdata = this.data.workdata
    workdata.work_intro[index] = e.detail.value
    this.setData({
      workdata:workdata
    })
  },
  inputRequest:function (e) {
    let index = e.currentTarget.dataset.index
    let workdata = this.data.workdata
    workdata.work_request[index] = e.detail.value
    this.setData({
      workdata:workdata
    })
  },
  inputSalary:function (e) {
    let index = e.currentTarget.dataset.index
    let workdata = this.data.workdata
    workdata.salary_detail[index] = e.detail.value
    this.setData({
      workdata:workdata
    })
  },
  // 点击增加按钮
  addline:function () {
    let workdata = this.data.workdata
    workdata.work_intro.push('')
    this.setData({
      workdata:workdata
    })
  },
  addrequest:function () {
    let workdata = this.data.workdata
    workdata.work_request.push('')
    this.setData({
      workdata:workdata
    })
  },
  addsalary:function () {
    let workdata = this.data.workdata
    workdata.salary_detail.push('')
    this.setData({
      workdata:workdata
    })
  },
  // 点击删除按钮
  minusline:function (e) {
    let index = e.currentTarget.dataset.index
    let workdata = this.data.workdata
    workdata.work_intro.splice(index,1)
    this.setData({
      workdata:workdata
    })
  },
  minusrequest:function (e) {
    let index = e.currentTarget.dataset.index
    let workdata = this.data.workdata
    workdata.work_request.splice(index,1)
    this.setData({
      workdata:workdata
    })
  },
  minussalary:function (e) {
    let index = e.currentTarget.dataset.index
    let workdata = this.data.workdata
    workdata.salary_detail.splice(index,1)
    this.setData({
      workdata:workdata
    })
  },
  uploadTeamImg() {
    let that = this;
    let workdata = this.data.workdata
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        });
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let filePath = res.tempFilePaths[0];
        const name = Math.random() * 1000000;
        const cloudPath = name + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath: 'companyImgs/'+cloudPath,//云存储图片名字
          filePath,//临时路径
          success: res => {
            console.log('[上传图片] 成功：', res)
            workdata.img = res.fileID//云存储图片路径,可以把这个路径存到集合，要用的时候再取出来
            that.setData({
              workdata: workdata,
            });
          },
           fail: e => {
            console.error('[上传图片] 失败：', e)
          },
          complete: () => {
            wx.hideLoading()
          }
        });
      }
    })
  },
  delWork(){
    let id = this.data.id
    wx.showModal({
      title:'提示',
      content:'确定删除该实习岗位吗？',
      showCancel: true,
        cancelText: '取消',
        confirmText: '确定',
        success: (result) => {
          if (result.confirm) {
            wx.cloud.database().collection('internlist').where({_id:id}).remove({
              success:res=>{
                wx.showToast({
                  title: '删除成功',
                })
                wx.navigateBack()
              }
            })
          }
        },
        fail: () => {
          return false
        },
        complete: () => {}
    })
  },
  formSubmit: function (e) {
    let data = e.detail.value
    let id = this.data.id,workdata=this.data.workdata
    delete workdata['_id']
    delete workdata['_openid']
    workdata.work_request = data.work_request.split('\n')
    workdata.work_intro = data.work_intro.split('\n')
    workdata.salary_detail = data.salary_detail.split('\n')

    wx.cloud.database().collection('internlist').doc(id).update({
      data:workdata,
      success:res=>{
        console.log(res)
        wx.showToast({
          title: '上传成功',
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})